//
//  UIView+view.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "UIView+view.h"
#import <objc/runtime.h>

static char tapKey;

@implementation UIView (view)


- (void)makeRoundingCorners:(UIRectCorner)corners cornerRadii:(CGFloat)cornerRadii
{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(cornerRadii, cornerRadii)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}


#pragma mark - 添加单击手势
- (void)tapHandle:(TapAction)block {
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self addGestureRecognizer:tap];
    objc_setAssociatedObject(self, &tapKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    TapAction blcok = objc_getAssociatedObject(self, &tapKey);
    if (blcok) {
        blcok();
    }
}

+ (UIView *)ha_createView {
    return  [[UIView alloc]init];
}

+ (UIImageView *)ha_createImageView {
    return  [[UIImageView alloc]init];
}


+(UILabel *)ha_NLabelWithFont:(CGFloat)font textColor:(id)textColor text:(NSString *)text alignment:(NSTextAlignment)alignment {
    UILabel *laebl = [[UILabel alloc]init];
    laebl.text = text;
    if ([textColor isKindOfClass:[UIColor class]]) {
        laebl.textColor = textColor;
    }else if ([textColor isKindOfClass:[NSString class]]) {
        laebl.textColor = [UIColor colorWithHexString:textColor];
    }
    laebl.font = [UIFont systemFontOfSize:font];
    laebl.textAlignment = alignment;
    return laebl;
}

+(UILabel *)ha_BLabelWithFont:(CGFloat)font textColor:(id)textColor text:(NSString *)text alignment:(NSTextAlignment)alignment {
    UILabel *laebl = [[UILabel alloc]init];
    laebl.text = text;
    if ([textColor isKindOfClass:[UIColor class]]) {
        laebl.textColor = textColor;
    }else if ([textColor isKindOfClass:[NSString class]]) {
        laebl.textColor = [UIColor colorWithHexString:textColor];
    }
    laebl.font = [UIFont boldSystemFontOfSize:font];
    laebl.textAlignment = alignment;
    return laebl;
}


@end
